<!DOCTYPE html>
<html lang="en" xmlns:sa="http://www.thymeleaf.org/extras/sa-token"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>电信引流内容备案管理</title>
    <!--公共模块-->
    <div th:replace="~{layout}"></div>
</head>
<body>
<div class="panel panel-default operation_telecom_content_record" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="info" style="width: 900px;margin-top: 10px">
            <input name="id" hidden/>
            
            <!-- 第一行 -->
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red;">*</span>省份</label>
                        <div class="layui-input-block">
                            <input type="text" name="province" placeholder="请输入省份" autocomplete="off" class="layui-input" lay-verify="required">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red;">*</span>主端口号</label>
                        <div class="layui-input-block">
                            <input type="number" name="mainPort" placeholder="请输入主端口号" autocomplete="off" class="layui-input" lay-verify="required|number" min="1">
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 第二行 -->
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red;">*</span>子端口号</label>
                        <div class="layui-input-block">
                            <input type="number" name="subPort" placeholder="请输入子端口号" autocomplete="off" class="layui-input" lay-verify="required|number" min="1">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">号码1</label>
                        <div class="layui-input-block">
                            <input type="text" name="number1" placeholder="请输入号码1" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 第三行 -->
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">号码2</label>
                        <div class="layui-input-block">
                            <input type="text" name="number2" placeholder="请输入号码2" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">链接1</label>
                        <div class="layui-input-block">
                            <input type="text" name="link1" placeholder="请输入链接1" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 第四行 -->
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red;">*</span>操作类型</label>
                        <div class="layui-input-block">
                            <select name="operationType" lay-verify="required">
                                <option value="">请选择操作类型</option>
                                <option value="1">1-新增修改</option>
                                <option value="2">2-删除</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <select name="status">
                                <option value="0">待提交</option>
                                <option value="1">已提交</option>
                                <option value="2">已备案</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 第五行 -->
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用途说明</label>
                        <div class="layui-input-block">
                            <textarea name="usageDescription" placeholder="请输入用途说明" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="telecom_content_record_table_div">
    <div id="searchParam">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="text" name="province" class="layui-input" autocomplete="off" placeholder="请输入省份">
                </div>
                <div class="layui-input-inline">
                    <input type="text" name="mainPort" class="layui-input" autocomplete="off" placeholder="请输入主端口号">
                </div>
                <div class="layui-input-inline">
                    <input type="text" name="subPort" class="layui-input" autocomplete="off" placeholder="请输入子端口号">
                </div>
                <div class="layui-input-inline layui-form">
                    <select name="operationType">
                        <option value="">请选择操作类型</option>
                        <option value="1">1-新增修改</option>
                        <option value="2">2-删除</option>
                    </select>
                </div>
                <div class="layui-input-inline layui-form">
                    <select name="status">
                        <option value="">请选择状态</option>
                        <option value="0">待提交</option>
                        <option value="1">已提交</option>
                        <option value="2">已备案</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon">&#xe615;</i> 搜 索</button>
                </div>
            </div>
        </form>
    </div>
    
    <table class="layui-hide" id="telecom_content_record_table" lay-filter="telecom_content_record_table"></table>
</div>

<!-- 隐藏的文件上传输入框 -->
<input type="file" id="importFile" accept=".xlsx,.xls" style="display: none;">

<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addNewTelecomContentRecord">新增</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="importData">导入</button>
        <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="refreshData">刷新</button>
        <button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="batchToPending">批量待提交</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="batchToSubmitted">批量已提交</button>
        <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="batchToFiled">批量已备案</button>
    </div>
</script>

<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="toPending">待提交</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="toSubmitted">已提交</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="toFiled">已备案</a>
</script>

<script type="text/html" id="statusTpl">
    {{# if(d.status == 0) { }}
        <span class="layui-badge layui-bg-orange">待提交</span>
    {{# } else if(d.status == 1) { }}
        <span class="layui-badge layui-bg-blue">已提交</span>
    {{# } else if(d.status == 2) { }}
        <span class="layui-badge layui-bg-green">已备案</span>
    {{# } else { }}
        <span class="layui-badge layui-bg-gray">未知</span>
    {{# } }}
</script>

<script type="text/html" id="operationTypeTpl">
    {{# if(d.operationType == 1) { }}
        <span class="layui-badge layui-bg-blue">1-新增修改</span>
    {{# } else if(d.operationType == 2) { }}
        <span class="layui-badge layui-bg-red">2-删除</span>
    {{# } else { }}
        <span class="layui-badge layui-bg-gray">未知</span>
    {{# } }}
</script>

<script th:inline="none">
    layui.use(['table', 'form', 'layer'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;

        var tableIns = table.render({
            elem: '#telecom_content_record_table'
            , contentType: 'application/json'
            , page: true
            , url: ctx + 'telecomContentRecord/listByPage'
            , method: 'POST'
            , parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data ? res.data.total : 0,
                    "data": res.data ? res.data.records : []
                }
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'province', title: '省份', width: 120},
                    {field: 'mainPort', title: '主端口号', width: 120},
                    {field: 'subPort', title: '子端口号', width: 120},
                    {field: 'number1', title: '号码1', width: 150},
                    {field: 'number2', title: '号码2', width: 150},
                    {field: 'link1', title: '链接1', width: 200},
                    {field: 'operationType', title: '操作类型', width: 120, templet: '#operationTypeTpl'},
                    {field: 'status', title: '状态', width: 100, templet: '#statusTpl'},
                    {field: 'createTime', title: '创建时间', width: 160},
                    {field: 'updateTime', title: '更新时间', width: 160},
                    {title: '操作', width: 350, toolbar: '#tool', fixed: 'right'}
                ]
            ]
            , toolbar: '#toolbar'
        });

        // 工具栏事件
        table.on('toolbar(telecom_content_record_table)', function (obj) {
            switch (obj.event) {
                case 'addNewTelecomContentRecord':
                    $(".telecom_content_record_table_div").hide();
                    $(".operation_telecom_content_record").show();
                    $(".title").html("新增电信引流内容备案");
                    form.val('info', {
                        "id": "",
                        "province": "",
                        "mainPort": "",
                        "subPort": "",
                        "number1": "",
                        "number2": "",
                        "link1": "",
                        "usageDescription": "",
                        "operationType": "",
                        "status": "0"
                    });
                    
                    // 新增时移除主端口号和子端口号的只读属性
                    $('input[name="mainPort"]').removeAttr('readonly');
                    $('input[name="subPort"]').removeAttr('readonly');
                    
                    // 重新渲染表单
                    form.render();
                    break;
                case 'importData':
                    document.getElementById('importFile').click();
                    break;
                case 'refreshData':
                    tableIns.reload();
                    break;
                case 'batchToPending':
                    batchUpdateStatus(table.checkStatus('telecom_content_record_table').data, 0);
                    break;
                case 'batchToSubmitted':
                    batchUpdateStatus(table.checkStatus('telecom_content_record_table').data, 1);
                    break;
                case 'batchToFiled':
                    batchUpdateStatus(table.checkStatus('telecom_content_record_table').data, 2);
                    break;
            }
        });

        // 行工具事件
        table.on('tool(telecom_content_record_table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    layer.confirm('确定删除这条记录吗？', {icon: 3, title: '提示'}, function (index) {
                        layer.close(index);
                        CoreUtil.sendDelete(ctx + 'telecomContentRecord/delete/' + data.id, null, function (res) {
                            layer.msg(res.msg);
                            tableIns.reload();
                        });
                    });
                    break;
                case 'edit':
                    $(".telecom_content_record_table_div").hide();
                    $(".operation_telecom_content_record").show();
                    $(".title").html("编辑电信引流内容备案");
                    getTelecomContentRecordDetail(data.id);
                    break;
                case 'toPending':
                    updateStatus(data.id, 0, '待提交');
                    break;
                case 'toSubmitted':
                    updateStatus(data.id, 1, '已提交');
                    break;
                case 'toFiled':
                    updateStatus(data.id, 2, '已备案');
                    break;
            }
        });

        // 获取详情
        var getTelecomContentRecordDetail = function (id) {
            CoreUtil.sendGet(ctx + "telecomContentRecord/detail/" + id, null, function (res) {
                if (res.code === 0) {
                    form.val('info', res.data);
                    
                    // 编辑时设置主端口号和子端口号为只读
                    $('input[name="mainPort"]').attr('readonly', true);
                    $('input[name="subPort"]').attr('readonly', true);
                } else {
                    layer.msg('获取详情失败：' + res.msg);
                }
            });
        };

        // 更新状态
        var updateStatus = function (id, status, statusName) {
            var url = '';
            switch (status) {
                case 0:
                    url = 'telecomContentRecord/status/toPending/' + id;
                    break;
                case 1:
                    url = 'telecomContentRecord/status/toSubmitted/' + id;
                    break;
                case 2:
                    url = 'telecomContentRecord/status/toFiled/' + id;
                    break;
            }
            
            CoreUtil.sendPost(ctx + url, null, function (res) {
                layer.msg(res.msg);
                tableIns.reload();
            });
        };

        // 批量更新状态
        var batchUpdateStatus = function (data, status) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].id);
            }

            if (ids.length === 0) {
                layer.msg('请选择要批量操作的记录');
                return;
            }

            var statusName = '';
            switch (status) {
                case 0:
                    statusName = '待提交';
                    break;
                case 1:
                    statusName = '已提交';
                    break;
                case 2:
                    statusName = '已备案';
                    break;
            }

            layer.confirm('确定要将选中的 ' + ids.length + ' 条记录设置为 "' + statusName + '" 吗？', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                CoreUtil.sendPost(ctx + 'telecomContentRecord/status/batchUpdateStatus', {ids: ids, status: status}, function (res) {
                    layer.msg(res.msg);
                    tableIns.reload();
                });
            });
        };

        // 返回按钮
        $("#btn_cancel").click(function () {
            $(".telecom_content_record_table_div").show();
            $(".operation_telecom_content_record").hide();
            return false;
        });

        // 监听提交
        form.on('submit(submit)', function (data) {
            var params = data.field;
            
            if (params.id === undefined || params.id === null || params.id === "") {
                // 新增
                CoreUtil.sendPost(ctx + "telecomContentRecord/add", params, function (res) {
                    layer.msg(res.msg);
                    $(".telecom_content_record_table_div").show();
                    $(".operation_telecom_content_record").hide();
                    tableIns.reload();
                });
            } else {
                // 编辑
                CoreUtil.sendPost(ctx + "telecomContentRecord/update", params, function (res) {
                    layer.msg(res.msg);
                    $(".telecom_content_record_table_div").show();
                    $(".operation_telecom_content_record").hide();
                    tableIns.reload();
                });
            }
            return false;
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            tableIns.reload({
                page: {
                    curr: 1
                }
                , where: data.field
            });
            return false;
        });

        // 文件导入处理
        document.getElementById('importFile').onchange = function (e) {
            var file = e.target.files[0];
            if (!file) {
                return;
            }

            var formData = new FormData();
            formData.append('file', file);

            layer.load(1, {shade: [0.3, '#000']});

            fetch(ctx + 'telecomContentRecord/import', {
                method: 'POST',
                body: formData
            }).then(response => response.json()).then(result => {
                layer.closeAll('loading');
                if (result.code === 0) {
                    layer.msg('导入成功', {icon: 1});
                    tableIns.reload();
                } else {
                    layer.msg('导入失败：' + result.msg, {icon: 2});
                }
            }).catch(err => {
                layer.closeAll('loading');
                layer.msg('导入失败：' + err.message, {icon: 2});
            });

            e.target.value = '';
        };

        // 主端口号和子端口号输入限制：只允许输入数字
        $('input[name="mainPort"], input[name="subPort"]').on('input', function() {
            var value = $(this).val();
            // 移除非数字字符
            value = value.replace(/[^\d]/g, '');
            $(this).val(value);
        });
    });
</script>

</body>
</html>


